/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string): boolean {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str: string): boolean {
  const valid_map = ['admin', 'editor', 'enterprise', 'project']
  return valid_map.indexOf(str.trim()) >= 0
}
/**
 * @description 手机号
 * @param {string} str
 * @returns {Boolean}
 */
export function checkPhone(mobile: string): boolean {
  const phone = /^[1][3-9][0-9]{9}$/
  if (phone.test(mobile)) {
    return true
  }
  return false
}
/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url: string): boolean {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str: string): boolean {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str: string): boolean {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str: string): boolean {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email: string): boolean {
  const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str: string): boolean {
  if (typeof str === 'string') {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray<T>(arg: Array<T>): boolean {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}
// 用户姓名验证
export const userfullname = (rule: any, value: any, callback: Function): any => {
  if (!value) {
    callback(new Error('输入不能为空'))
  } else {
    callback()
  }
}
// 用户身份证的验证
export const useridnumber = (rule: any, value: any, callback: Function): any => {
  if (!value) {
    callback(new Error('输入不能为空'))
  } else {
    if (value.length < 18) {
      callback(new Error('输入身份证号长度不足18位'))
    } else {
      callback()
    }
  }
}
// 企业码验证
export const userentcode = (rule: any, value: any, callback: Function): any => {
  if (!value) {
    callback(new Error('输入不能为空'))
  } else {
    callback()
  }
}
// 手机号验证
export const userIsphone = (rule: any, value: any, callback: Function): any => {
  if (!value) {
    callback(new Error('输入不能为空'))
  } else {
    const phoneTest = /^1[3456789]\d{9}$/
    if (!phoneTest.test(value)) {
      callback(new Error('手机号格式错误'))
    } else {
      callback()
    }
  }
}
// 验证码验证
export const usercode = (rule: any, value: any, callback: Function): any => {
  if (!value) {
    callback(new Error('输入不能为空'))
  } else {
    if (value.length < 4) {
      callback(new Error('输入验证码长度不足4位'))
    } else {
      callback()
    }
  }
}
// 图片格式验证 jpg,png.word.ppt.excel.pdf

export function validImage(str: string) {
  const _t = str.toLowerCase()
  return /(.*)\.(jpg|jpeg|png)$/.test(_t)
}
//上传验证
export const validateFrontSideLicense = (rule: any, value: any, callback: Function): any => {
  console.log(value)
  if (value.length == 0) {
    callback(new Error(rule.message))
  } else {
    callback()
  }
}

/**
 * @description 表单验证
 */
// 手机号验证
export const validateTel = (rule: any, value: any, callback: Function): any => {
  if (!value) {
    callback(new Error('手机号不能为空！'))
  } else if (!checkPhone(value)) {
    callback(new Error('请输入正确手机号！'))
  } else {
    callback()
  }
}

// 密码验证验证
export const validatePassword = (rule: any, value: any, callback: Function): any => {
  if (value.length < 6) {
    callback(new Error('密码错误！'))
  } else {
    callback()
  }
}

export const validateConfirmPassword = (dialogForm: any, rule: any, value: any, callback: Function): any => {
  let newPwd = dialogForm.value.newPwd
  if (value.length < 8) {
    callback(new Error('密码错误！'))
  } else if (value !== newPwd) {
    callback(new Error('密码不一致！'))
  } else {
    callback()
  }
}

// 整数验证
export function validateInteger(rule: any, value: any, callback: Function): any {
  if (!value) {
    return callback(new Error('输入不可以为空'))
  }
  setTimeout(() => {
    if (!Number(value)) {
      callback(new Error('请输入正整数'))
    } else {
      const re = /^[0-9]*[1-9][0-9]*$/
      const rsCheck = re.test(value)
      if (!rsCheck) {
        callback(new Error('请输入正整数'))
      } else if (value > 9999999) {
        callback(new Error('最大可输入9999999万'))
      } else {
        callback()
      }
    }
  }, 0)
}

// 金额验证
export function validatePrice(price: number | string | any, maxPrice = 9999999999) {
  const reg = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^[1-9]$)|(^\d\.[1-9]{1,2}$)|(^\d\.[0]{1}[1-9]{1}$|(^\d\.[1-9]{1}[0]{1}$)$)/
  let validate = true
  if (!reg.test(price) || price > maxPrice) {
    validate = false
  }
  return validate
}
// 验证密码 必须包含数字、大小写字母、特殊符号
export const validatePwd = (rule: any, value: any, callback: Function): any => {
  if (!value) {
    return callback()
  } else if (value.length < 10 || value.length > 20) {
    return callback(new Error('密码长度应在 10 到 20 个字符'))
  } else {
    const reg = /(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{10,20}/
    if (reg.test(value)) {
      callback()
    } else {
      return callback(new Error('密码必须包含大小写字母、数字、特殊字符4种'))
    }
  }
}
